package com.wye.controller;

import com.wye.file.FastDFSFile;
import com.wye.util.FastDFSUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "/upload")
@CrossOrigin
public class FileUploadController {

    /**
     * 文件上传
     */
    @PostMapping
    public Result upload(@RequestParam(value = "file")MultipartFile file) throws Exception {

        //封装文件信息
        FastDFSFile fastDFSFile = new FastDFSFile();
        fastDFSFile.setName(file.getOriginalFilename());
        fastDFSFile.setContent(file.getBytes());
        String ext = StringUtils.getFilenameExtension(file.getOriginalFilename());
        fastDFSFile.setExt(ext);
        //文件上传
        String[] uploads = FastDFSUtil.upload(fastDFSFile);
        String url = "http://192.168.0.109:8080/" +uploads[0] + "/" +uploads[1];
        return new Result(true, StatusCode.OK, "上传成功", url);
    }
}
